package com.fafa.yygh.hosp.controller.api;

import com.alibaba.excel.util.StringUtils;
import com.fafa.yygh.common.exception.YyghException;
import com.fafa.yygh.common.helper.HttpRequestHelper;
import com.fafa.yygh.common.result.Result;
import com.fafa.yygh.common.result.ResultCodeEnum;
import com.fafa.yygh.common.utils.MD5;
import com.fafa.yygh.hosp.service.DepartmentService;
import com.fafa.yygh.hosp.service.HospitalService;
import com.fafa.yygh.hosp.service.HospitalSetService;
import com.fafa.yygh.hosp.service.ScheduleService;
import com.fafa.yygh.model.hosp.Department;
import com.fafa.yygh.model.hosp.Hospital;
import com.fafa.yygh.model.hosp.HospitalSet;
import com.fafa.yygh.model.hosp.Schedule;
import com.fafa.yygh.vo.hosp.DepartmentQueryVo;
import com.fafa.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Sire
 * @version 1.0
 * @date 2022-02-10 15:01
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 上传医院
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "上传医院")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        // 将请求参数变成map
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 将 String[] 转成 object(不然需要自己手动遍历)
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        // 传来的签名(已加密处理过的)
        String hospSign = (String) map.get("sign");
        // 要和数据库里的sign_Key对比(让相应的医院调用对应的接口)
        String hoscode = (String) map.get("hoscode");
        // 调用service
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 将数据库查询签名进行MD5加密处理
        String signKeyMd5 = MD5.encrypt(signKey);
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String) map.get("logoData");
        if (!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            map.put("logoData", logoData);
        }
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        // 存到MongoDB中
        hospitalService.save(map);
        return Result.ok();
    }

    /**
     * 查询医院信息
     *
     * @return
     */
    @ApiOperation(value = "查询医院")
    @PostMapping("/hospital/show")
    public Result getHospitalById(HttpServletRequest request) {
        // 将请求参数变成map
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 将 String[] 转成 object(不然需要自己手动遍历)
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        // 传来的签名(已加密处理过的)
        String hospSign = (String) map.get("sign");
        // 要和数据库里的sign_Key对比(让相应的医院调用对应的接口)
        String hoscode = (String) map.get("hoscode");
        // 调用service
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 将数据库查询签名进行MD5加密处理
        String signKeyMd5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        Hospital byHoscode = hospitalService.getByHoscode(hoscode);
        return Result.ok(byHoscode);
    }

    /**
     * 上传科室信息
     */
    @ApiOperation(value = "上传科室信息")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        // 将请求参数变成map
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 将 String[] 转成 object(不然需要自己手动遍历)
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        // 传来的签名(已加密处理过的)
        String hospSign = (String) map.get("sign");
        // 要和数据库里的sign_Key对比(让相应的医院调用对应的接口)
        String hoscode = (String) map.get("hoscode");
        // 调用service
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 将数据库查询签名进行MD5加密处理
        String signKeyMd5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(map);
        return Result.ok();
    }

    /**
     * 查询科室(分页)
     */
    @ApiOperation(value = "查询科室")
    @PostMapping("/department/list")
    public Result getDepartment(HttpServletRequest request) {
        // 将请求参数变成map
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 将 String[] 转成 object(不然需要自己手动遍历)
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        // 传来的签名(已加密处理过的)
        String hospSign = (String) map.get("sign");
        // 要和数据库里的sign_Key对比(让相应的医院调用对应的接口)
        String hoscode = (String) map.get("hoscode");
        // 科室编号
        String depcode = (String) map.get("depcode");
        // 调用service
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 将数据库查询签名进行MD5加密处理
        String signKeyMd5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        // 当前页（从 1 开始）
        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String) map.get("page"));
        // 每页大小
        int limit = StringUtils.isEmpty(map.get("limit")) ? 10 : Integer.parseInt((String) map.get("limit"));
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);
        Page<Department> departments = departmentService.selectPage(page, limit, departmentQueryVo);
        return Result.ok(departments);
    }

    /**
     * 删除科室信息（通过id）
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "删除科室信息（通过id）")
    @PostMapping("/department/remove")
    public Result deleteDepartmentById(HttpServletRequest request) {
        // 将请求参数变成map
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 将 String[] 转成 object(不然需要自己手动遍历)
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        // 传来的签名(已加密处理过的)
        String hospSign = (String) map.get("sign");
        // 要和数据库里的sign_Key对比(让相应的医院调用对应的接口)
        String hoscode = (String) map.get("hoscode");
        // 科室编号
        String depcode = (String) map.get("depcode");
        // 调用service
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 将数据库查询签名进行MD5加密处理
        String signKeyMd5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.removeDepartment(hoscode, depcode);
        return Result.ok();
    }

    /**
     * 上传排班信息
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "上传排班信息")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        // 将请求参数变成map
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 将 String[] 转成 object(不然需要自己手动遍历)
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        // 传来的签名(已加密处理过的)
        String hospSign = (String) map.get("sign");
        // 要和数据库里的sign_Key对比(让相应的医院调用对应的接口)
        String hoscode = (String) map.get("hoscode");
        // 调用service
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 将数据库查询签名进行MD5加密处理
        String signKeyMd5 = MD5.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        // 调用service层
        scheduleService.save(map);
        return Result.ok();
    }

    /**
     * 查询排班信息
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "上传排班信息")
    @PostMapping("/schedule/list")
    public Result getSchedule(HttpServletRequest request) {
        // 将请求参数变成map
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 将 String[] 转成 object(不然需要自己手动遍历)
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        // 传来的签名(已加密处理过的)
        String hospSign = (String) map.get("sign");
        // 要和数据库里的sign_Key对比(让相应的医院调用对应的接口)
        String hoscode = (String) map.get("hoscode");
        // 科室编号
        String depcode = (String) map.get("depcode");
        // 调用service
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 将数据库查询签名进行MD5加密处理
        String signKeyMd5 = MD5.encrypt(signKey);
        // 当前页
        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String) map.get("page"));
        // 每页大小
        int limit = StringUtils.isEmpty(map.get("limit")) ? 10 : Integer.parseInt((String) map.get("limit"));
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> schedules = scheduleService.selectPage(page, limit, scheduleQueryVo);
        return Result.ok(schedules);
    }

    /**
     * 删除排班信息
     */
    @ApiOperation(value = "删除排班信息")
    @PostMapping("/schedule/remove")
    public Result deleteSchedule(HttpServletRequest request) {
        // 将请求参数变成map
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 将 String[] 转成 object(不然需要自己手动遍历)
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        // 传来的签名(已加密处理过的)
        String hospSign = (String) map.get("sign");
        // 要和数据库里的sign_Key对比(让相应的医院调用对应的接口)
        String hoscode = (String) map.get("hoscode");
        // 科室编号
        String hosScheduleId = (String) map.get("hosScheduleId");
        // 调用service
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 将数据库查询签名进行MD5加密处理
        String signKeyMd5 = MD5.encrypt(signKey);
        // 当前页
        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String) map.get("page"));
        // 每页大小
        int limit = StringUtils.isEmpty(map.get("limit")) ? 10 : Integer.parseInt((String) map.get("limit"));
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.removeSchedule(hoscode, hosScheduleId);
        return Result.ok();
    }
}
